package sub2;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.LockModeType;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 * Define the entity "Fichier". The shared files are stored in the server directory (using JPA)
 * @author Magali Moulin, Pierre Labiausse
 * @version 2
 */
@NamedQueries({
	@NamedQuery(
			name = "findFichierWithFilename",
			query = "SELECT fichier FROM Fichier fichier WHERE fichier.filename LIKE :filename",
			lockMode = LockModeType.OPTIMISTIC_FORCE_INCREMENT),
	@NamedQuery(
			name = "listFichiers",
			query = "SELECT fichier FROM Fichier fichier",
			lockMode = LockModeType.OPTIMISTIC_FORCE_INCREMENT)
})

@Entity(name="Fichier")
@Table(name = "fichier")
public class Fichier{
	@Id
	@Column (name="id", nullable=false)
	@GeneratedValue (strategy=GenerationType.SEQUENCE)
	private long fileID;
	
	@Version
	private int version;
	
	@Column(name = "filename", nullable = false)
	private String filename;
	
	@ManyToMany(
			cascade = {CascadeType.PERSIST,CascadeType.MERGE })
	
	private Set<Peer> peers;
	
	/**
	 * Getter to have access to the name of the file
	 * @return filename: name of the file
	 */
	public String getFilename() {
		return filename;
	}
	/**
	 * Constructor 
	 */
	public Fichier(){}
	
	/**
	 * Constructor of this class which takes the name of the file in parameter
	 * @param filename: name of the file
	 */
	public Fichier(String filename){
		this.filename = filename;
	}
	
	/**
	 * Constructor of this class which takes the name of the file and the set of peers who share this file in parameter
	 * @param peer : set of peers who share the file
	 * @param filename: name of the file
	 */
	public Fichier( Set<Peer> peer, String filename){
		this.filename = filename;
		this.peers = peer;
	}
	
	/**
	 * Getter to have access to a set of peers 
	 * @return peers : set of peers 
	 */
	public Set<Peer> getPeers() {
		return peers;
	}
	
	/**
	 * Setter to add a set of peers
	 * @param peer: set of peers
	 */
	public void setPeer(Set<Peer> peer) {
		this.peers = peer;
	}
	
	
}

